package quanlythuvien

import org.springframework.dao.DataIntegrityViolationException

class NhanVienController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


	/*
	 * To do logout system
	 * */
	def doLogout(){
		def user = session.user
		
		if (user){
			session.user = null
		}
		
		redirect(controller:"Index", action:"Index", params:params)
	}

	def doLogin () {
		
		def email = params.uname
		def pass = params.matKhau
		
		def user = NhanVien.findWhere(email:email,
				matKhau:pass)
		
		if (user)
		{
			session.user = user								
		}
		
		redirect(controller:"Index", action:"Index", params:params)
	}

	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[nhanVienInstanceList: NhanVien.list(params), nhanVienInstanceTotal: NhanVien.count()]
	}

	def create() {
		[nhanVienInstance: new NhanVien(params)]
	}

	def save() {
		def nhanVienInstance = new NhanVien(params)
		if (!nhanVienInstance.save(flush: true)) {
			render(view: "create", model: [nhanVienInstance: nhanVienInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'nhanVien.label', default: 'NhanVien'),
			nhanVienInstance.id
		])
		redirect(action: "show", id: nhanVienInstance.id)
	}

	def show() {
		def nhanVienInstance = NhanVien.get(params.id)
		if (!nhanVienInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'nhanVien.label', default: 'NhanVien'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[nhanVienInstance: nhanVienInstance]
	}

	def edit() {
		def nhanVienInstance = NhanVien.get(params.id)
		if (!nhanVienInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'nhanVien.label', default: 'NhanVien'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[nhanVienInstance: nhanVienInstance]
	}

	def update() {
		def nhanVienInstance = NhanVien.get(params.id)
		if (!nhanVienInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'nhanVien.label', default: 'NhanVien'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (nhanVienInstance.version > version) {
				nhanVienInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'nhanVien.label', default: 'NhanVien')]
						as Object[],
						"Another user has updated this NhanVien while you were editing")
				render(view: "edit", model: [nhanVienInstance: nhanVienInstance])
				return
			}
		}

		nhanVienInstance.properties = params

		if (!nhanVienInstance.save(flush: true)) {
			render(view: "edit", model: [nhanVienInstance: nhanVienInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'nhanVien.label', default: 'NhanVien'),
			nhanVienInstance.id
		])
		redirect(action: "show", id: nhanVienInstance.id)
	}

	def delete() {
		def nhanVienInstance = NhanVien.get(params.id)
		if (!nhanVienInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'nhanVien.label', default: 'NhanVien'),
				params.id
			])
			redirect(action: "list")
			return
		}

		try {
			nhanVienInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'nhanVien.label', default: 'NhanVien'),
				params.id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'nhanVien.label', default: 'NhanVien'),
				params.id
			])
			redirect(action: "show", id: params.id)
		}
	}
}
